import os
import json
import requests
from datetime import datetime

path = 'allure-results'
result = {'passed': 0, 'failed': 0}
for filename in os.listdir(path):
    if filename.lower().endswith('json'):
        with open(path + '/' + filename, 'r', encoding='utf-8') as f:
            a = f.read()
            report = json.loads(a)
            if report['status'] == 'passed':
                result['passed'] += 1
            else:
                result['failed'] += 1
        f.close()
total = result['passed'] + result['failed']
rate = f"{(result['passed'] / total):.2%}"
headers = {'Content-Type': 'application/json'}
today = datetime.now().strftime('%Y-%m-%d')
data = {"msg_type": "text", "content": {
    "text": f"{today}自动化用例一共执行{total}个,通过的有{result['passed']}个, 失败的有{result['failed']}个，成功率为{rate}"}}
url = 'https://open.feishu.cn/open-apis/bot/v2/hook/f0fcdebb-e829-4710-a282-b42dfef69783'
response = requests.post(url=url, data=json.dumps(data), headers=headers)
print(json.loads(response.text))
